﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace CSharpLearning.AsyncOperation
{
    public class AsyncHelper
    {
        public void AsyncTest()
        {
            try
            {
                Console.WriteLine("Welcome to this application.");

                IAsyncResult async = null;
                SqlDataReader reader = null;

                var Cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString);

                var Cmd = Cnn.CreateCommand();
                Cmd.CommandText = "Select * From TvShows";
                Cmd.CommandType = CommandType.Text;

                Cnn.Open();
                async = Cmd.BeginExecuteReader();
                while (!async.IsCompleted)
                {
                    Console.WriteLine("Waiting...");
                }
                reader = Cmd.EndExecuteReader(async);
                while (reader.Read())
                {
                    Console.WriteLine(reader["Name"].ToString());
                }
                Cnn.Close();

                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception:", ex.ToString());
            }
        }
    }
}
